﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class WorkOrderListControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnAddWorkOrder_Click(object sender, EventArgs e)
        {

        }
        protected void lnkViewMap_Click(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int WorkOrderid = Convert.ToInt32(e.CommandArgument.ToString());

            ucModalViewMapControl.ShowMapDataByWorkOrderID(companyid, WorkOrderid);
        }
        protected void gvWorkOrderList_Select(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int WorkOrderid = Convert.ToInt32(e.CommandArgument.ToString());
            int locationid = RSSBMCommonFunctions.GetLocationIDByWorkOrderID(userIdentity.RSCompanyId, WorkOrderid);
            Response.Redirect("RSWorkOrderDetail.aspx?locationid=" + locationid + "&workorderid=" + WorkOrderid);
        }
        protected void gvWorkOrderList_Delete(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int WorkOrderid = Convert.ToInt32(e.CommandArgument.ToString());
                if (companyid > 0 && WorkOrderid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@WorkOrderid", SqlDbType.Int, 0, ParameterDirection.Input, WorkOrderid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblWorkOrder WHERE workorderid=@WorkOrderid AND companyid=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    ucModalActionMessageControl.ShowModal("Work Order deleted successfully!", Request.RawUrl, true);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        public void LoadWorkOrderListByLocationID(int companyid, int locationid)
        {
            lblEstimate.Text = "Location Work Order History";
            ddlWorkOrderStatus.Visible = false;
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "SELECT p.WorkOrderId,p.IsInvoiced,p.WorkOrderName,p.Description,p.Response,p.Active,p.StartDT,p.EndDT,(select t.typename from rs_tblType t where t.typeid=p.WorkOrderTypeId) as WorkOrderType,p.AssignTo,(select s.statusname from rs_tblStatus s where s.statusid=p.WorkOrderstatusid) as Status,p.ProjectId,p.LocationId,p.CompanyId FROM rs_tblWorkOrder p where p.LocationId=@locationid and p.CompanyId=@CompanyId order by p.startdt, p.enddt desc";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@locationid";
            param.Value = locationid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvWorkOrderListGrid.DataSource = ds;
            gvWorkOrderListGrid.DataBind();
            myConnection.Close();

        }
        public void LoadWorkOrderListByAll(int companyid, string sql_and = "")
        {
            lblEstimate.Text = "Status:";
            ddlWorkOrderStatus.Visible = true;
            if (!Page.IsPostBack)
            {
                RSSBMDataLookupLoader.LoadStatusDDL(companyid, "WORKORDER_CAT", ddlWorkOrderStatus, "All", "0");
                ddlWorkOrderStatus.SelectedValue = "0";
                btnAddWorkOrder.Visible = false;
            }

            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            sql_and = sql_and.Trim();

            String sqlSELECT = "SELECT p.WorkOrderId,p.IsInvoiced,p.WorkOrderName,p.Description,p.Response,p.Active,p.StartDT,p.EndDT,(select t.typename from rs_tblType t where t.typeid=p.WorkOrderTypeId) as WorkOrderType,p.AssignTo,(select s.statusname from rs_tblStatus s where s.statusid=p.WorkOrderstatusid) as Status,p.ProjectId,p.LocationId,p.CompanyId FROM rs_tblWorkOrder p where p.CompanyId=@CompanyId " + sql_and + " order by p.startdt, p.enddt desc";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            Session["DATATABLE"] = ds.Tables[0];
            gvWorkOrderListGrid.DataSource = ds;
            gvWorkOrderListGrid.DataBind();
            myConnection.Close();

        }

        protected void ddlWorkOrderStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            string sql_and = "";
            int isel = Convert.ToInt32(ddlWorkOrderStatus.SelectedValue);
            if (isel > 0)
                sql_and = "AND WorkOrderStatusId=" + isel;
            LoadWorkOrderListByAll(companyid, sql_and);
        }

        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }

        protected void gvWorkOrderListGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvWorkOrderListGrid.PageIndex = e.NewPageIndex;
            gvWorkOrderListGrid.DataBind();
        }

        protected void gvWorkOrderListGrid_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = Session["DATATABLE"] as DataTable;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvWorkOrderListGrid.DataSource = dataView;
                gvWorkOrderListGrid.DataBind();
            }
        }
    }
}